package com.car.web.controller.index;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.car.bean.GridObj;
import com.car.bean.PageObj;
import com.car.dal.dao.department.TgDepartmentDao;
import com.car.dal.dao.from.FromUserInURLDao;
import com.car.dal.dao.moneys.TgMoneyDao;
import com.car.dal.dao.office.TgOfficeDao;
import com.car.dal.dao.performance.PerformanceAcountDao;
import com.car.dal.dao.reception.TgReceptionDao;
import com.car.dal.dao.reserve.TgReserveDao;
import com.car.dal.dao.source.TgSourceDao;
import com.car.dal.dao.teacher.TeacherDao;
import com.car.dal.dao.teaching.TeachingDao;
import com.car.dal.dao.user.QqgroupDao;
import com.car.dal.dao.user.UserDao;
import com.car.dal.dao.user.UserinfoDao;
import com.car.dal.dao.user.UsernamesDao;
import com.car.dal.dao.usergroup.TgUsergroupDao;
import com.car.dal.dao.vote.JsVoteDao;
import com.car.dal.dao.work.TgworkDao;
import com.car.dal.object.JsFromUser;
import com.car.dal.object.JsVote;
import com.car.dal.object.QqGroup;
import com.car.dal.object.Record;
import com.car.dal.object.Teacher;
import com.car.dal.object.TgDepartment;
import com.car.dal.object.TgMoney;
import com.car.dal.object.TgOffice;
import com.car.dal.object.TgPerformanceAmount;
import com.car.dal.object.TgReception;
import com.car.dal.object.TgReserve;
import com.car.dal.object.TgSource;
import com.car.dal.object.TgStudent;
import com.car.dal.object.TgTeaching;
import com.car.dal.object.TgUser;
import com.car.dal.object.TgUsergroup;
import com.car.dal.object.TgUserinfo;
import com.car.dal.object.TgUsernames;
import com.car.dal.object.TgWork;
import com.car.exception.BaseException;
import com.car.service.reception.ReceptionService;
import com.car.util.JsonExecutor;
import com.car.util.JsonExecutor.JsonBiz;
import com.car.util.DateUtil;
import com.car.util.HttpUtil;
import com.car.util.MD5;
import com.car.util.MyFileUtil;
import com.car.util.Page;
import com.car.util.PageUtil;
import com.car.util.Result;
import com.car.util.ResultSupport;
import com.car.util.StringUtil;
import com.car.util.UserUtil;
import com.car.util.WebExecutor;
import com.car.util.WebExecutor.Biz;
import com.car.web.controller.base.BaseController;
import com.car.web.controller.base.BaseVo;
import com.google.gson.Gson;
import com.ssi.utils.SsiUtils;

import net.sf.json.JsonConfig;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 登录控制
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/index")
public class UserController extends BaseController {

	private static final Logger logger = Logger.getLogger(UserController.class);
	@Resource
	private UserDao userDao;
	@Resource
	private TgworkDao workDao;
	@Resource
	private TeacherDao teacherDao;
	@Resource
	private TgUsergroupDao tgUsergroupDao;
	@Resource
	private TgDepartmentDao tgDepartmentDao;
	@Resource
	private UserinfoDao userinfoDao;
	@Resource
	private TgOfficeDao tgOfficeDao;
	@Resource
	private TeachingDao teachingDao;
	@Resource
	private TgSourceDao sourceDao;
	@Resource
	private FromUserInURLDao fromUserInRulDao;
	@Resource
	private JsVoteDao voteDao;
	@Resource
	private QqgroupDao groupDao;
	@Resource
	private UsernamesDao usernamesDao;
	@Resource
	private ReceptionService receptionService;
	@Resource
	private TgReceptionDao receptionDao;
	@Resource
	private TgReserveDao reserveDao;
	@Resource
	private TgMoneyDao moneyDao;
	@Resource
	private PerformanceAcountDao performanceAcountDao;

	// 组织架构--我的资料信息
	@RequestMapping(value = "/struct_info")
	public ModelAndView struct_info(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					TgUser user = UserUtil.getUserFromCookies();
					TgUserinfo userInfo = new TgUserinfo();
					userInfo.setUsername(user.getUsername());
					List<TgUserinfo> infoList = userinfoDao.findByExample(userInfo);
					if (infoList != null && infoList.size() > 0) {
						TgDepartment tgDepartment = new TgDepartment();
						tgDepartment.setKeyname(infoList.get(0).getDepartment());
						List<TgDepartment> departmentList = tgDepartmentDao.findByExample(tgDepartment);
						if (departmentList != null && departmentList.size() > 0) {
							result.setDefaultModel("departmentName", departmentList.get(0).getName());
						}

						Integer currPage = 1;
						if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
							currPage = vo.getCurrPage();
						}
						TgUserinfo userinfo = new TgUserinfo();
						userinfo.setId(infoList.get(0).getId());
						Integer total = userinfoDao.getUserInfoCount(userinfo);
						Page page = PageUtil.createPage(8, currPage, total);
						List<TgUserinfo> list = userinfoDao.getUserInfoByUserName(userinfo, page);
						if (list != null && list.size() > 0) {
							userInfo = list.get(0);
							Long birthday = userInfo.getBirthday() == null ? 0 : userInfo.getBirthday();
							Long entryday = userInfo.getEntryday() == null ? 0 : userInfo.getEntryday();
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
							userInfo.setBirthdays(sdf.format(new Date(birthday * 1000)));
							userInfo.setEntrydays(sdf.format(new Date(entryday * 1000)));

							result.setDefaultModel("userInfo", userInfo);
						}
					}
					List<TgOffice> office_List = tgOfficeDao.findAll(TgOffice.class);
					result.setDefaultModel("office_List", office_List);

					TgDepartment tgDepartment = new TgDepartment();
					tgDepartment.setParent("0");
					List<TgDepartment> departmentList = tgDepartmentDao.findByExample(tgDepartment);
					result.setDefaultModel("departmentList", departmentList);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("struct/struct_info", modelMap);
	}

	// 组织架构--员工通讯录
	@RequestMapping(value = "/struct_addbook")
	public ModelAndView struct_addbook(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					String nickName = request.getParameter("nickName");
					String centName = request.getParameter("center");
					String type = request.getParameter("type");

					TgUserinfo userinfo = new TgUserinfo();
					if (nickName != null && !"".equals(nickName) && !"null".equals(nickName)) {
						// nickName = new String(nickName.getBytes("iso-8859-1"), "utf-8");
						userinfo.setNickname(nickName);
						result.setDefaultModel("nickName", nickName);
					}
					if (centName != null && !"".equals(centName) && !"null".equals(centName)) {
						// centName = new String(centName.getBytes("iso-8859-1"), "utf-8");
						userinfo.setRegion(centName);
						result.setDefaultModel("centName", centName);
					}
					if (type != null && !"".equals(type)) {
						userinfo.setZhuanz_date("1");
					}
					TgUser user = UserUtil.getUserFromCookies();
					if (user != null) {
						Integer currPage = 1;
						if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
							currPage = vo.getCurrPage();
						}

						Integer total = userinfoDao.getUserInfoCount(userinfo);
						Page page = PageUtil.createPage(8, currPage, total);
						List<TgUserinfo> list = userinfoDao.getUserInfoByUserName(userinfo, page);

						result.setDefaultModel("num", total);
						result.setDefaultModel("page", page);
						result.setDefaultModel("list", list);
						result.setDefaultModel("type", type);
						List<TgOffice> office_List = tgOfficeDao.findAll(TgOffice.class);
						result.setDefaultModel("office_List", office_List);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("struct/struct_addbook", modelMap);
	}

	// 获取职位
	@RequestMapping(value = "/getDepartment")
	public ModelAndView getDepartment(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String keyname = request.getParameter("keyname");
				TgDepartment tgDepartment = new TgDepartment();
				tgDepartment.setKeyname(keyname);
				List<TgDepartment> departmentList = tgDepartmentDao.findByExample(tgDepartment);
				if (departmentList != null && departmentList.size() > 0) {
					TgDepartment tgDepartment2 = new TgDepartment();
					tgDepartment2.setParent(departmentList.get(0).getKeyname());
					departmentList = tgDepartmentDao.findByExample(tgDepartment2);
					result.setDefaultModel("departmentList", departmentList);
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 修改个人资料信息
	@RequestMapping(value = "/updateInfo")
	public ModelAndView addGoodsApply(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				TgUserinfo userInfo = new TgUserinfo();

				TgUser user = UserUtil.getUserFromCookies();
				if (user != null) {
					userInfo.setId(user.getId());
				}

				String nickname = request.getParameter("nickname");
				String department = request.getParameter("department");
				String sex = request.getParameter("sex");
				String nation = request.getParameter("nation");
				String marriage = request.getParameter("marriage");
				String region = request.getParameter("region");
				String idnum = request.getParameter("idnum");
				String workexp = request.getParameter("workexp");
				String birthday = request.getParameter("birthday");
				String entryday = request.getParameter("entryday");
				String hometown = request.getParameter("hometown");
				String census = request.getParameter("census");
				String address = request.getParameter("address");
				String phone = request.getParameter("phone");
				String email = request.getParameter("email");
				String contact = request.getParameter("contact");
				String contact_phone = request.getParameter("contact_phone");
				String education = request.getParameter("education");
				String certificate = request.getParameter("certificate");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

				userInfo.setNickname(nickname);
				if (department != null && !"".equals(department)) {
					userInfo.setDepartment(department);
				}
				userInfo.setSex(sex);
				userInfo.setNation(nation);
				userInfo.setMarriage(marriage);
				userInfo.setRegion(region);
				userInfo.setIdnum(idnum);
				if (workexp != null && !"".equals(workexp)) {
					userInfo.setWorkexp(Integer.parseInt(workexp));
				}
				if (birthday != null && !"".equals(birthday) && !"1970-01-01".equals(birthday)) {
					try {
						Date date = sdf.parse(birthday);
						userInfo.setBirthday(date.getTime() / 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				if (entryday != null && !"".equals(entryday) && !"1970-01-01".equals(birthday)) {
					try {
						Date date = sdf.parse(entryday);
						userInfo.setEntryday(date.getTime() / 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}

				}
				userInfo.setHometown(hometown);
				userInfo.setCensus(census);
				userInfo.setAddress(address);
				userInfo.setPhone(phone);
				userInfo.setEmail(email);
				userInfo.setContact(contact);
				userInfo.setContact_phone(contact_phone);
				userInfo.setEducation(education);
				userInfo.setCertificate(certificate);

				Boolean falg = userinfoDao.update(userInfo);
				result.setDefaultModel("flag", falg);
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 组织架构--部门结构
	@RequestMapping(value = "/struct_department")
	public ModelAndView struct_department(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					TgDepartment department = new TgDepartment();
					department.setParent("0");
					List<TgDepartment> depList = tgDepartmentDao.findByExample(department);
					if (depList != null && depList.size() > 0) {
						for (TgDepartment depa : depList) {
							TgDepartment department2 = new TgDepartment();
							department2.setParent(depa.getKeyname());
							List<TgDepartment> depList2 = tgDepartmentDao.findByExample(department2);
							depa.setList(depList2);
						}
					}
					result.setDefaultModel("depList", depList);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("struct/struct_department", modelMap);
	}

	// 组织架构--部门结构
	@RequestMapping(value = "/struct_department_new")
	public ModelAndView struct_department_new(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					TgDepartment department = new TgDepartment();
					department.setParent("0");
					List<TgDepartment> depList = tgDepartmentDao.findByExample(department);
					if (depList != null && depList.size() > 0) {
						TgDepartment department2 = new TgDepartment();
						TgUser user = new TgUser();
						for (TgDepartment depa : depList) {
							department2.setParent(depa.getKeyname());
							List<TgDepartment> depList2 = tgDepartmentDao.findByExample(department2);
							depa.setList(depList2);

							if (depList2 != null && depList2.size() > 0) {
								for (TgDepartment d : depList2) {
									user.setJob(d.getName());
									d.setUlist(userDao.getUserInfoByType2(user));
								}
							}

						}
					}

					result.setDefaultModel("depList", depList);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("struct/struct_department_new", modelMap);
	}

	// 组织架构--qq分组
	@RequestMapping(value = "/struct_qq_group")
	public ModelAndView struct_qq_group(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				QqGroup group = new QqGroup();
				List<QqGroup> list = groupDao.findByExample(group);
				if (list != null && list.size() > 0) {
					TgUser user = new TgUser();
					for (QqGroup q : list) {
						if (!q.getName().equals("竞思") && q.getId() != null && !"".equals(q.getId())) {
							user.setQq_group(q.getId());
							q.setList(userDao.getUserInfoByType2(user));
						}
					}
				}
				result.setDefaultModel("list", list);
				return result;
			}
		});
		return new ModelAndView("struct/struct_qq_group", modelMap);
	}

	// 组织架构--qq分组添加
	@RequestMapping(value = "/qq_group_add")
	public ModelAndView qq_group_add(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String parent = request.getParameter("parent");
				String name = request.getParameter("name");

				QqGroup group = new QqGroup();
				if (parent != null && !"".equals(parent)) {
					group.setParent(Long.valueOf(parent));
					group.setName(name);
					group.setCreate_time(new Date());
					groupDao.insert(group);
					result.setDefaultModel("msg", "1");
				}

				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 组织架构--qq分组修改
	@RequestMapping(value = "/qq_group_update")
	public ModelAndView qq_group_update(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");
				String name = request.getParameter("name");

				QqGroup group = new QqGroup();
				if (id != null && !"".equals(id)) {
					group.setId(Long.valueOf(id));
					group.setName(name);
					groupDao.update(group);
					result.setDefaultModel("msg", "1");
				}

				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 组织架构--添加职位
	@RequestMapping(value = "/depar_add")
	public ModelAndView depar_add(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");
				String name = request.getParameter("name");

				TgDepartment department = new TgDepartment();
				if (id != null && !"".equals(id)) {
					TgDepartment department2 = tgDepartmentDao.findById(TgDepartment.class, Long.valueOf(id));
					if (department2 != null) {
						department.setName(name);
						department.setParent(department2.getKeyname());
						department.setKeyname(SsiUtils.randomUUID());
						tgDepartmentDao.insert(department);
						result.setDefaultModel("msg", "1");
					}
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 组织架构--修改职位
	@RequestMapping(value = "/depar_upd")
	public ModelAndView depar_upd(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");
				String name = request.getParameter("name");
				if (id != null && !"".equals(id)) {
					TgDepartment department = tgDepartmentDao.findById(TgDepartment.class, Long.valueOf(id));
					if (department != null) {
						department.setName(name);
						tgDepartmentDao.update(department);
						result.setDefaultModel("msg", "1");
					}
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 组织架构--删除职位
	@RequestMapping(value = "/depar_del")
	public ModelAndView depar_del(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");

				if (id != null && !"".equals(id)) {
					TgDepartment department = tgDepartmentDao.findById(TgDepartment.class, Long.valueOf(id));
					if (department != null) {
						if (!department.getParent().equals("0")) {
							tgDepartmentDao.delete(TgDepartment.class, Long.valueOf(id));
							result.setDefaultModel("msg", "1");
						} else {
							result.setDefaultModel("msg", "2");
						}
					}
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 添加或修改部门结构
	@RequestMapping(value = "/addDepartment")
	public ModelAndView addDepartment(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				// 添加参数
				String type = request.getParameter("type");
				String name = request.getParameter("name");
				String pkey = request.getParameter("pkey");
				// 修改参数
				String key = request.getParameter("key");
				TgDepartment department = new TgDepartment();
				if (type.equals("add")) {
					if (pkey.equals("root")) {
						department.setKeyname(SsiUtils.randomUUID());
						department.setParent("0");
						department.setName(name);
					} else {
						department.setKeyname(SsiUtils.randomUUID());
						department.setParent(pkey);
						department.setName(name);
					}
					tgDepartmentDao.insert(department);
				} else if (type.equals("modify")) {
					department.setKeyname(key);
					List<TgDepartment> list = tgDepartmentDao.findByExample(department);
					if (list != null && list.size() > 0) {
						department.setId(list.get(0).getId());
						department.setName(name);
						tgDepartmentDao.update(department);
					}
				} else if (type.equals("del")) {
					department.setKeyname(key);
					List<TgDepartment> list = tgDepartmentDao.findByExample(department);
					if (list != null && list.size() > 0) {
						department.setId(list.get(0).getId());
						tgDepartmentDao.delete(department);
					}
				}

				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 系统设置--添加新账号
	@RequestMapping(value = "/admin_user_new")
	public ModelAndView admin_user_new(ModelMap modelMap, final LoginVo loginVo, HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, loginVo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				TgDepartment department = new TgDepartment();
				department.setParent("0");
				List<TgDepartment> depList = tgDepartmentDao.findByExample(department);

				List<TgOffice> offList = tgOfficeDao.findAll(TgOffice.class);

				List<TgUsergroup> groupList = tgUsergroupDao.findAll(TgUsergroup.class);

				List<QqGroup> qqlist = groupDao.findAll(QqGroup.class);

				result.setDefaultModel("groupList", groupList);
				result.setDefaultModel("depList", depList);
				result.setDefaultModel("offList", offList);
				result.setDefaultModel("qqlist", qqlist);
				return result;
			}
		});
		return new ModelAndView("system/admin_user_new", modelMap);
	}

	// 添加用户
	@RequestMapping(value = "/addUser")
	public ModelAndView addUser(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				// 添加参数
				String nickname = request.getParameter("nickname");
				String job = request.getParameter("job");
				String region = request.getParameter("region");
				String usergroup = request.getParameter("usergroup");
				String qq_group_id = request.getParameter("qq_group_id");
				String username = request.getParameter("username");
				String password = request.getParameter("password");

				TgUser user = new TgUser();
				user.setUsername(username);
				List<TgUser> list = userDao.findByExample(user);
				if (list != null && list.size() > 0) {
					result.setDefaultModel("flag", "0");
					return result;
				} else {
					if (usergroup != null && !"".equals(usergroup)) {
						user.setUsergroup(Integer.parseInt(usergroup));
					}
					String salt = "HKUUKOIIKJKLLI";
					String dbPasswordMd5 = MD5.md5(password + salt);
					user.setPassword(dbPasswordMd5);
					user.setLocked(0);
					user.setCreatedate(new Date().getTime() / 1000);
					userDao.insert(user);

					TgUserinfo userInfo = new TgUserinfo();
					userInfo.setUsername(username);
					userInfo.setNickname(nickname);
					userInfo.setQq_group(Long.valueOf(qq_group_id));
					userInfo.setRegion(region);
					userInfo.setDepartment(job);
					userInfo.setMdate(new Date().getTime() / 1000);
					userinfoDao.insert(userInfo);
					result.setDefaultModel("flag", "1");
				}

				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 系统设置--职员账号管理
	@RequestMapping(value = "/system_userlist")
	public ModelAndView system_userlist(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					String nickName = request.getParameter("nickName");
					String centName = request.getParameter("center");
					String type = request.getParameter("type");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

					TgUserinfo userinfo = new TgUserinfo();
					if (nickName != null && !"".equals(nickName) && !"null".equals(nickName)) {
						userinfo.setNickname(nickName);
						result.setDefaultModel("nickName", nickName);
					}
					if (type != null && !"".equals(type) && !"null".equals(type)) {
						userinfo.setType(Integer.valueOf(type));

						try {

							String url = "http://www.yueqiao.org/web/teaweb/zhurenList.json?position=1";
							String teacher = HttpUtil.doGet(url, "", "utf-8", true);
							JSONObject obj = new JSONObject(teacher);

							JSONArray listobj = obj.getJSONArray("list");
							List<TgUser> tlist = new ArrayList<TgUser>();
							for (int i = 0; i < listobj.length(); i++) {
								TgUser tuser = new TgUser();
								JSONObject o = new JSONObject(listobj.get(i).toString());
								if (!o.isNull("name")) {
									tuser.setNickname(o.getString("name"));
								}
								if (!o.isNull("login_name")) {
									tuser.setUsername(o.getString("login_name"));
								}
								if (!o.isNull("center")) {
									tuser.setRegions(o.getString("center"));
								}
								if (!o.isNull("create_time")) {
									tuser.setParent(sdf.format(new Date(Long.valueOf(o.getString("create_time")))));
								}
								tlist.add(tuser);
							}

							result.setDefaultModel("tlist", tlist);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						result.setDefaultModel("type", type);
					}
					if (centName != null && !"".equals(centName) && !"null".equals(centName)) {
						userinfo.setRegion(centName);
						result.setDefaultModel("centName", centName);
					}
					TgUser user = UserUtil.getUserFromCookies();
					if (user != null) {
						Integer currPage = 1;
						if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
							currPage = vo.getCurrPage();
						}

						Integer total = userinfoDao.getUserInfoCount(userinfo);
						Page page = PageUtil.createPage(8, currPage, total);
						List<TgUserinfo> list = userinfoDao.getUserInfoByUserName(userinfo, page);

						if (list != null && list.size() > 0) {
							TgUser u = new TgUser();
							for (TgUserinfo userInfo : list) {
								u.setUsername(userInfo.getUsername());
								List<TgUser> uList = userDao.findByExample(u);
								if (uList != null && uList.size() > 0) {
									if (uList.get(0).getCreatedate() != null
											&& !"".equals(uList.get(0).getCreatedate())) {
										String createTime = sdf.format(new Date(uList.get(0).getCreatedate() * 1000));
										userInfo.setCreateTime(createTime);
									}
								}
							}
						}

						result.setDefaultModel("page", page);
						result.setDefaultModel("list", list);

						List<TgOffice> office_List = tgOfficeDao.findAll(TgOffice.class);
						result.setDefaultModel("office_List", office_List);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("system/system_userlist", modelMap);
	}

	// 系统设置--进入修改页面
	@RequestMapping(value = "/system_user_base_modify")
	public ModelAndView system_person_month_stat(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					String username = request.getParameter("username");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					TgUser user = new TgUser();
					user.setUsername(username);
					List<TgUser> userList = userDao.findByExample(user);
					if (userList != null && userList.size() > 0) {
						result.setDefaultModel("user", userList.get(0));
					}

					TgUserinfo userInfo = new TgUserinfo();
					if (username != null && !"".equals(username)) {
						userInfo.setUsername(username);
					}
					List<TgUserinfo> infoList = userinfoDao.findByExample(userInfo);
					if (infoList != null && infoList.size() > 0) {
						TgDepartment tgDepartment = new TgDepartment();
						tgDepartment.setKeyname(infoList.get(0).getDepartment());
						List<TgDepartment> departmentList = tgDepartmentDao.findByExample(tgDepartment);
						if (departmentList != null && departmentList.size() > 0) {
							result.setDefaultModel("departmentName", departmentList.get(0).getName());
						}

						Integer currPage = 1;
						if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
							currPage = vo.getCurrPage();
						}
						TgUserinfo userinfo = new TgUserinfo();
						userinfo.setId(infoList.get(0).getId());
						Integer total = userinfoDao.getUserInfoCount(userinfo);
						Page page = PageUtil.createPage(8, currPage, total);
						List<TgUserinfo> list = userinfoDao.getUserInfoByUserName(userinfo, page);
						if (list != null && list.size() > 0) {
							userInfo = list.get(0);
							Long birthday = userInfo.getBirthday() == null ? 0 : userInfo.getBirthday();
							Long entryday = userInfo.getEntryday() == null ? 0 : userInfo.getEntryday();
							userInfo.setBirthdays(sdf.format(new Date(birthday * 1000)));
							userInfo.setEntrydays(sdf.format(new Date(entryday * 1000)));

							result.setDefaultModel("userInfo", userInfo);
						}
					}

					// 职位
					TgDepartment department = new TgDepartment();
					department.setParent("0");
					List<TgDepartment> depList = tgDepartmentDao.findByExample(department);
					// 中心地址
					List<TgOffice> offList = tgOfficeDao.findAll(TgOffice.class);
					// 用户组
					List<TgUsergroup> groupList = tgUsergroupDao.findAll(TgUsergroup.class);
					// qq分组
					List<QqGroup> qqlist = groupDao.findAll(QqGroup.class);
					// 所有
					TgUser user2 = new TgUser();
					user2.setJob("课程顾问");
					List<TgUser> ulist = userDao.getUserInfoByType2(user2);

					result.setDefaultModel("groupList", groupList);
					result.setDefaultModel("depList", depList);
					result.setDefaultModel("offList", offList);
					result.setDefaultModel("qqlist", qqlist);
					result.setDefaultModel("ulist", ulist);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("system/system_user_base_modify", modelMap);
	}

	// 系统设置--修改用户信息
	@RequestMapping(value = "/updateUser")
	public ModelAndView updateUser(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			@SuppressWarnings("unused")
			public Result process() {
				Result result = new ResultSupport(true);
				// 添加参数
				String type = request.getParameter("type");
				String nickname = request.getParameter("nickname");
				String job = request.getParameter("job");
				String region = request.getParameter("region");
				String usergroup = request.getParameter("usergroup");
				String qq_group_id = request.getParameter("qq_group_id");
				String entryday = request.getParameter("entryday");
				String zhuanz_date = request.getParameter("zhuanz_date");
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				String locked = request.getParameter("locked");

				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

				TgUser user = new TgUser();
				user.setUsername(username);
				List<TgUser> list = userDao.findByExample(user);
				if (list != null && list.size() > 0) {
					if (usergroup != null && !"".equals(usergroup)) {
						user.setUsergroup(Integer.parseInt(usergroup));
					}
					user.setId(list.get(0).getId());
					if (!password.equals("") && password != null) {
						String salt = "HKUUKOIIKJKLLI";
						user.setPassword(MD5.md5(password + salt));
					}
					/*
					 * if(dbPasswordMd5!=null && !"".equals(dbPasswordMd5)){
					 * 
					 * }
					 */
					if (locked != null && !"".equals(locked)) {
						user.setLocked(Integer.parseInt(locked));
					}
					userDao.update(user);

					TgUserinfo userInfo = new TgUserinfo();
					userInfo.setUsername(username);
					List<TgUserinfo> infoList = userinfoDao.findByExample(userInfo);
					userInfo.setId(infoList.get(0).getId());
					if (type != null && !"".equals(type)) {
						userInfo.setType(Integer.valueOf(type));
					} else {
						userInfo.setType(0);
					}
					if (nickname != null && !"".equals(nickname)) {
						userInfo.setNickname(nickname);
					}
					if (region != null && !"".equals(region)) {
						userInfo.setRegion(region);
					}
					if (job != null && !"".equals(job)) {
						userInfo.setDepartment(job);
					}
					if (qq_group_id != null && !"".equals(qq_group_id)) {
						userInfo.setQq_group(Long.valueOf(qq_group_id));
					}
					if (zhuanz_date != null && !"".equals(zhuanz_date)) {
						userInfo.setZhuanz_date(zhuanz_date);
					}
					if (entryday != null && !"".equals(entryday)) {
						try {
							userInfo.setEntryday(sdf.parse(entryday).getTime() / 1000);
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}
					userInfo.setMdate(new Date().getTime() / 1000);
					userinfoDao.update(userInfo);
					result.setDefaultModel("flag", "1");
				}

				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 系统设置--查看用户详情
	@RequestMapping(value = "/system_userinfo")
	public ModelAndView system_userinfo(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					String username = request.getParameter("username");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

					TgUser user = new TgUser();
					user.setUsername(username);
					List<TgUser> userList = userDao.findByExample(user);
					if (userList != null && userList.size() > 0) {
						result.setDefaultModel("user", userList.get(0));
					}

					TgUserinfo userInfo = new TgUserinfo();
					userInfo.setUsername(username);
					List<TgUserinfo> infoList = userinfoDao.findByExample(userInfo);
					if (infoList != null && infoList.size() > 0) {
						TgDepartment tgDepartment = new TgDepartment();
						tgDepartment.setKeyname(infoList.get(0).getDepartment());
						List<TgDepartment> departmentList = tgDepartmentDao.findByExample(tgDepartment);
						if (departmentList != null && departmentList.size() > 0) {
							result.setDefaultModel("departmentName", departmentList.get(0).getName());
						}

						Integer currPage = 1;
						if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
							currPage = vo.getCurrPage();
						}
						TgUserinfo userinfo = new TgUserinfo();
						userinfo.setId(infoList.get(0).getId());
						Integer total = userinfoDao.getUserInfoCount(userinfo);
						Page page = PageUtil.createPage(8, currPage, total);
						List<TgUserinfo> list = userinfoDao.getUserInfoByUserName(userinfo, page);
						if (list != null && list.size() > 0) {
							userInfo = list.get(0);

							Long birthday = userInfo.getBirthday() == null ? 0 : userInfo.getBirthday();
							Long entryday = userInfo.getEntryday() == null ? 0 : userInfo.getEntryday();

							userInfo.setBirthdays(sdf.format(new Date(birthday * 1000)));
							userInfo.setEntrydays(sdf.format(new Date(entryday * 1000)));

							result.setDefaultModel("userInfo", userInfo);
						}
					}

					// 职位
					TgDepartment department = new TgDepartment();
					department.setParent("0");
					List<TgDepartment> depList = tgDepartmentDao.findByExample(department);
					// 中心地址
					List<TgOffice> offList = tgOfficeDao.findAll(TgOffice.class);
					// 用户组
					List<TgUsergroup> groupList = tgUsergroupDao.findAll(TgUsergroup.class);

					result.setDefaultModel("groupList", groupList);
					result.setDefaultModel("depList", depList);
					result.setDefaultModel("offList", offList);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("system/system_userinfo", modelMap);
	}

	// 系统设置--用户组
	@RequestMapping(value = "/system_usergroup")
	public ModelAndView system_usergroup(ModelMap modelMap, final BaseVo vo, HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				List<TgUsergroup> list = tgUsergroupDao.findAll(TgUsergroup.class);
				result.setDefaultModel("list", list);
				return result;
			}
		});
		return new ModelAndView("system/system_usergroup", modelMap);
	}

	// 系统设置--查看每个中心的所有情况，包括人员，装修，物品等
	@RequestMapping(value = "/system_office")
	public ModelAndView system_office(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String center = request.getParameter("center");
				String keyname = request.getParameter("keyname");
				String center2 = "";
				if (center != null && !"".equals(center) && !"null".equals(center)) {
					center2 = center.substring(0, 2);
				}
				if (keyname != null && !"".equals(keyname)) {
					TgOffice office = new TgOffice();
					office.setKeyname(keyname);
					List<TgOffice> list = tgOfficeDao.findByExample(office);
					if (list != null && list.size() > 0) {
						try {
							office = list.get(0);
							String notice = list.get(0).getNotice();
							if (notice != null && !"".equals(notice)) {
								String content = "";
								if (keyname.length() >= 20) {
									content = notice;
								} else {
									boolean flag = StringUtil.getHanzi(notice);
									if (!flag) {
										byte[] bytes = StringUtil.hexDecode(notice);
										content = new String(bytes, "UTF-8");
									} else {
										content = notice;
									}
								}
								office = list.get(0);
								office.setNotice(content);
							}
							String type = request.getParameter("type");
							TgUserinfo userinfo = new TgUserinfo();
							userinfo.setLocked(0); // 0为在职人员
							if (center != null && !"".equals(center) && !"null".equals(center)) {
								userinfo.setRegion(center);
								result.setDefaultModel("centName", center);
							}
							if (type != null && !"".equals(type)) {
								userinfo.setZhuanz_date("1");
							}
							Integer currPage = 1;
							if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
								currPage = vo.getCurrPage();
								Integer total = userinfoDao.getUserInfoCount(userinfo);
								Page page = PageUtil.createPage(total, currPage, total);
								List<TgUserinfo> list2 = userinfoDao.getUserInfoByUserName(userinfo, page); // 查询oa系统里面各个中心的老师

								result.setDefaultModel("num", total);
								result.setDefaultModel("page", page);
								result.setDefaultModel("list", list2);
								result.setDefaultModel("type", type);
								result.setDefaultModel("office", office);

								// 按中心名字查询训练老师系统的老师
								center = URLEncoder.encode(center, "utf-8");
								String url0 = "http://www.yueqiao.org/web/teaweb/laoshiList.json?center=" + center;
								String teacher0 = HttpUtil.doGet(url0, "", "utf-8", true);
								JSONObject obj0 = new JSONObject(teacher0);
								JSONArray listobj0 = obj0.getJSONArray("list");
								for (int i = 0; i < listobj0.length(); i++) {
									TgUser tuser0 = new TgUser();
									JSONObject o = new JSONObject(listobj0.get(i).toString());
									if (!o.isNull("name")) {
										tuser0.setNickname(o.getString("name"));
									}
									if (!o.isNull("job")) {
										tuser0.setJob(o.getString("job"));
									}
									if (!o.isNull("center")) {
										tuser0.setLoghash(o.getString("center"));
									}
									if (!o.isNull("id")) {
										tuser0.setId(Integer.parseInt(o.getString("id")));
									}
									if (!o.isNull("phone")) {
										tuser0.setRegions(o.getString("phone"));
									}

									result.setDefaultModel("AllTealist", tuser0);
								}

								// 查询训练系统的中心主任
								String url = "http://www.yueqiao.org/web/teaweb/zhurenList.json?position=1&center="
										+ center;
								String teacher = HttpUtil.doGet(url, "", "utf-8", true);
								JSONObject obj = new JSONObject(teacher);
								JSONArray listobj = obj.getJSONArray("list");
								for (int i = 0; i < listobj.length(); i++) {
									TgUser tuser = new TgUser();
									JSONObject o = new JSONObject(listobj.get(i).toString());
									if (!o.isNull("name")) {
										tuser.setNickname(o.getString("name"));
									}
									if (!o.isNull("login_name")) {
										tuser.setUsername(o.getString("login_name"));
									}
									if (!o.isNull("center")) {
										tuser.setLoghash(o.getString("center"));
									}
									if (!o.isNull("id")) {
										tuser.setId(Integer.parseInt(o.getString("id")));
									}
									if (!o.isNull("phone")) {
										tuser.setRegions(o.getString("phone"));
									}

									result.setDefaultModel("tlist", tuser);
								}
								// 查询训练系统的区域主管
								center2 = URLEncoder.encode(center2, "utf-8");
								String url2 = "http://www.yueqiao.org/web/teaweb/zhuguanList.json?position=2&center="
										+ center2;
								String teacher2 = HttpUtil.doGet(url2, "", "utf-8", true);
								JSONObject obj2 = new JSONObject(teacher2);
								JSONArray listobj2 = obj2.getJSONArray("list");
								for (int i = 0; i < listobj2.length(); i++) {
									TgUser tuser = new TgUser();
									JSONObject o = new JSONObject(listobj2.get(i).toString());
									if (!o.isNull("name")) {
										tuser.setNickname(o.getString("name"));
									}
									if (!o.isNull("login_name")) {
										tuser.setUsername(o.getString("login_name"));
									}
									if (!o.isNull("id")) {
										tuser.setId(Integer.parseInt(o.getString("id")));
									}
									if (!o.isNull("phone")) {
										tuser.setRegions(o.getString("phone"));
									}
									result.setDefaultModel("listobj2", tuser);
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
				return result;
			}
		});
		return new ModelAndView("system/system_office", modelMap);
	}

	// 系统设置--竞思中心地址管理
	@RequestMapping(value = "/office_list")
	public ModelAndView office_list(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String center = request.getParameter("center");
				TgOffice office = new TgOffice();
				Integer currPage = 1;
				if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
					currPage = vo.getCurrPage();
				}
				if (center != null && !"".equals(center)) {
					String cityName = center.substring(0, 2);
					office.setCity(cityName);
					List<TgOffice> a = tgOfficeDao.getCoreNameByCity2(office);
					result.setDefaultModel("list", a);
				} else {
					Integer currPage1 = vo.getCurrPage() == null ? 1 : vo.getCurrPage();
					Integer total = tgOfficeDao.getCentercount(office);
					Page page = PageUtil.createPage(8, currPage1, total);
					List<TgOffice> list = tgOfficeDao.getcentersByType(office, page);
					page.setTotal(total);
					result.setDefaultModel("list", list);
					result.setDefaultModel("page", page);
				}
				return result;
			}
		});
		return new ModelAndView("system/office_list", modelMap);
	}

	// 系统设置--添加竞思中心地址管理--进入添加或修改页面
	@RequestMapping(value = "/add_office")
	public ModelAndView add_office(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String keyname = request.getParameter("keyname");
				if (keyname != null && !"".equals(keyname)) {
					TgOffice office = new TgOffice();
					office.setKeyname(keyname);
					List<TgOffice> list = tgOfficeDao.findByExample(office);
					if (list != null && list.size() > 0) {
						try {
							office = list.get(0);
							String notice = list.get(0).getNotice();

							if (notice != null && !"".equals(notice)) {
								String content = "";
								if (keyname.length() >= 20) {
									content = notice;
								} else {
									boolean flag = StringUtil.getHanzi(notice);
									if (!flag) {
										byte[] bytes = StringUtil.hexDecode(notice);
										content = new String(bytes, "UTF-8");
									} else {
										content = notice;
									}
								}
								office = list.get(0);
								office.setNotice(content);
							}
							// 拆分星期
							if (office.getRestday() != null && !"".equals(office.getRestday())) {
								String[] res = office.getRestday().split(",");
								if (res != null && res.length > 0) {
									for (int i = 0; i < res.length; i++) {
										if (res[i].equals("1")) {
											office.setWeek1("1");
										} else if (res[i].equals("2")) {
											office.setWeek2("2");
										} else if (res[i].equals("3")) {
											office.setWeek3("3");
										} else if (res[i].equals("4")) {
											office.setWeek4("4");
										} else if (res[i].equals("5")) {
											office.setWeek5("5");
										} else if (res[i].equals("6")) {
											office.setWeek6("6");
										} else if (res[i].equals("7")) {
											office.setWeek7("7");
										}
									}
								}
							}

							TgUserinfo userinfo = new TgUserinfo();
							userinfo.setJob("课程顾问");
							userinfo.setRegion(keyname);
							List<TgUserinfo> uList = userinfoDao.getUserInfoByUserInfo(userinfo);
							result.setDefaultModel("uList", uList);
							result.setDefaultModel("office", office);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
				return result;
			}
		});
		return new ModelAndView("system/office_add", modelMap);
	}

	// 系统设置--添加或修改中心地址
	@RequestMapping(value = "/office_add")
	public ModelAndView office_add(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				// 添加参数
				String keyname = request.getParameter("keyname");
				String name = request.getParameter("name");
				String address = request.getParameter("address");
				String inphone = request.getParameter("inphone");
				String outphone = request.getParameter("outphone");
				String restday = request.getParameter("restday");// 休息日
				String province = request.getParameter("province");
				String city = request.getParameter("city");
				String citys = request.getParameter("citys");
				String notice = request.getParameter("notice");
				// String synch = request.getParameter("synch");
				String unum = request.getParameter("unum");

				TgOffice office = new TgOffice();
				if (keyname != null && !"".equals(keyname)) {
					office.setKeyname(keyname);
				} else {
					office.setKeyname(SsiUtils.randomUUID().substring(0, 12));
				}
				List<TgOffice> list = tgOfficeDao.findByExample(office);
				if (list != null && list.size() > 0) {
					office.setId(list.get(0).getId());
				}
				office.setName(name);
				office.setAddress(address);
				office.setInphone(inphone);
				office.setOutphone(outphone);
				office.setRestday(restday);
				if (province != null && !"".equals(province)) {
					office.setProvince(province);
				}
				if (city != null && !"".equals(city)) {
					office.setCity(city);
				}
				office.setCitys(citys);
				office.setNotice(notice);
				if (unum != null && !"".equals(unum)) {
					office.setUnum(Integer.parseInt(unum));
				}
				tgOfficeDao.saveOrUpdate(office);
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 系统设置-查询可同时测试数量
	@RequestMapping(value = "/query_num")
	public ModelAndView query_num(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			HttpServletResponse response) throws BaseException {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String username = request.getParameter("username");

				TgUserinfo userinfo = new TgUserinfo();
				if (username != null && !"".equals(username)) {
					userinfo.setUsername(username);
				}

				List<TgUserinfo> userlist = userinfoDao.findByExample(userinfo);
				int num = 0;
				if (userlist != null && userlist.size() > 0) {
					if (userlist.get(0).getUnum() != null) {
						num = userlist.get(0).getUnum();
					}

				}
				result.setDefaultModel("num", num);
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 模糊查找用户
	@RequestMapping(value = "/getUserByNickname")
	public ModelAndView getUserBynickname(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");

				TgUserinfo userinfo = new TgUserinfo();
				userinfo.setNickname(nickname);
				List<TgUserinfo> userlist = userinfoDao.getUserlistByType(userinfo);
				if (userlist != null && userlist.size() > 0) {
					result.setDefaultModel("user", userlist.get(0));
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	@RequestMapping(value = "/getUserByNickname1")
	public ModelAndView getUserBynickname1(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");

				TgUserinfo userinfo = new TgUserinfo();
				userinfo.setNickname(nickname);
				List<TgUserinfo> userlist = userinfoDao.getUserlistByType(userinfo);
				if (userlist != null && userlist.size() > 0) {
					result.setDefaultModel("user", userlist.get(0));
				} else {
					if (nickname != null && !"".equals(nickname)) {
						try {
							nickname = URLEncoder.encode(nickname, "utf-8");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
					} else {
						nickname = "";
					}
					String url = "http://www.yueqiao.org/web/period/period_teacher.json?nickname=" + nickname;
					String teacher = HttpUtil.doGet(url, "", "utf-8", true);
					JSONObject obj;
					try {
						obj = new JSONObject(teacher);
						JSONArray listobj = obj.getJSONArray("list");
						List<TgUser> tlist = new ArrayList<TgUser>();
						for (int i = 0; i < listobj.length(); i++) {
							TgUser tuser = new TgUser();
							JSONObject o = new JSONObject(listobj.get(i).toString());
							if (!o.isNull("name")) {
								tuser.setNickname(o.getString("name"));
							}
							if (!o.isNull("login_name")) {
								tuser.setUsername(o.getString("login_name"));
							}
							if (!o.isNull("center")) {
								tuser.setRegions(o.getString("center"));
							}
							tlist.add(tuser);
						}
						if (tlist != null && tlist.size() > 0) {
							result.setDefaultModel("user", tlist.get(0));
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 模糊查找用户
	@RequestMapping(value = "/getUserByNickname2")
	public ModelAndView getUserBynickname2(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");

				TgUser user = new TgUser();
				user.setNickname(nickname);
				List<TgUser> userlist = userDao.getUserInfoByType3(user);
				if (userlist != null && userlist.size() > 0) {
					result.setDefaultModel("user", userlist.get(0));
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 获取带教表数据
	@RequestMapping(value = "/teaching_list")
	public ModelAndView teaching_list(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					String core = request.getParameter("core");
					String teaching = request.getParameter("teaching");
					TgTeaching tgTeaching = new TgTeaching();
					if (core != null && !"".equals(core) && !"null".equals(core)) {
						tgTeaching.setCore(core);
						result.setDefaultModel("core", core);
					}
					if (teaching != null && !"".equals(teaching) && !"null".equals(teaching)) {
						tgTeaching.setTeaching(teaching);
						result.setDefaultModel("teaching", teaching);
					}
					TgUser user = UserUtil.getUserFromCookies();
					if (user != null) {
						Integer currPage = 1;
						if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
							currPage = vo.getCurrPage();
						}
						Integer total = teachingDao.getTgTeachingCount(tgTeaching);
						Page page = PageUtil.createPage(8, currPage, total);
						List<TgTeaching> list = teachingDao.getTgTeachingList(tgTeaching, page);
						result.setDefaultModel("num", total);
						result.setDefaultModel("page", page);
						result.setDefaultModel("list", list);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("teaching/teaching_list", modelMap);
	}

	/**
	 * 导入Excel (带教表)
	 */
	@RequestMapping(value = "/read_excel")
	public ModelAndView read_excel(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response, @RequestParam("file") final MultipartFile file) throws Exception {

		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String contextPath = "";
				try {
					// 获取输出流
					long date = new Date().getTime();
					String fileName = file.getOriginalFilename();
					contextPath = request.getSession().getServletContext().getRealPath("/") + date;
					File fileCatalog = new File(contextPath);
					if (!fileCatalog.exists()) {
						fileCatalog.mkdir();
					}

					OutputStream os = new FileOutputStream(contextPath + "/" + fileName);
					// 获取输入流 CommonsMultipartFile 中可以直接得到文件的流
					InputStream is = file.getInputStream();
					int temp;
					// 一个一个字节的读取并写入
					while ((temp = is.read()) != (-1)) {
						os.write(temp);
					}
					os.flush();
					os.close();
					is.close();
					InputStream stream = new FileInputStream(new File(contextPath + "/" + fileName));
					String[] split = fileName.split("\\.");
					String fileType = split[1];
					Workbook wb = null;
					if (fileType.equals("xls")) {
						wb = new HSSFWorkbook(stream);
					} else if (fileType.equals("xlsx")) {
						wb = new XSSFWorkbook(stream);
					}
					// 删除之前的数据 只保留最新
					teachingDao.deleteAllTeaching();
					TgTeaching tgTeaching = null;
					tgTeaching = new TgTeaching();
					Sheet sheet1 = wb.getSheetAt(0);
					for (int i = 1; i <= sheet1.getLastRowNum(); i++) {
						tgTeaching.setCore(cellIsEmpty(sheet1.getRow(i).getCell(0))); // 中心
						tgTeaching.setTeachingName(cellIsEmpty(sheet1.getRow(i).getCell(1))); // 咨询老师
						tgTeaching.setTeaching(cellIsEmpty(sheet1.getRow(i).getCell(2))); // 咨询老师账户
						tgTeaching.setConsultName(cellIsEmpty(sheet1.getRow(i).getCell(3))); // 带教老师
						tgTeaching.setConsult(cellIsEmpty(sheet1.getRow(i).getCell(4))); // 带教老师账户
						teachingDao.addTgTeaching(tgTeaching);
					}
					stream.close();
					result.setDefaultModel("statu", "success");
					result.setDefaultModel("rowNum", sheet1.getLastRowNum() + "");
					MyFileUtil.deleteAllFilesOfDir(new File(contextPath));
				} catch (Exception e) {
					e.printStackTrace();
					result.setDefaultModel("statu", "error");
					MyFileUtil.deleteAllFilesOfDir(new File(contextPath));
				}
				return result;
			}
		});
		return new ModelAndView("teaching/teaching_list", modelMap);

	}

	// 将单元格为null时装换成""
	public String cellIsEmpty(Cell cell) {
		if (cell == null) {
			return "";
		} else {
			return cell.getStringCellValue();
		}
	}

	@RequestMapping(value = "/from_user_url_list")
	public ModelAndView from_user_url_list(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String phone = request.getParameter("phone");
				String st_type = request.getParameter("st_type");
				String state = request.getParameter("state");
				String startTime = request.getParameter("startTime");
				String endTime = request.getParameter("endTime");
				String url = request.getParameter("url");
				SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
				JsFromUser fromUser = new JsFromUser();
				if (phone != null && !"".equals(phone)) {
					fromUser.setPhone(phone);
				}
				if (st_type != null && !"".equals(st_type)) {
					fromUser.setSt_type(Integer.valueOf(st_type));
				}
				if (startTime != null && !"".equals(startTime)) {
					result.setDefaultModel("startTime", startTime);
					try {
						fromUser.setStartTime(sd.parse(startTime).getTime() / 1000 + "");
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				if (endTime != null && !"".equals(endTime)) {
					result.setDefaultModel("endTime", endTime);
					try {
						fromUser.setEndTime(DateUtil.getTommorrowDay1(sd.parse(endTime)).getTime() / 1000 + "");
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (state != null && !"".equals(state)) {
					if (state.equals("1")) {
						fromUser.setRkey1("1");
					} else if (state.equals("2")) {
						fromUser.setRkey2("1");
					}
					fromUser.setState(state);
				}
				if (url != null && !"".equals(url)) {
					fromUser.setUrl(url);
				}

				Integer currPage = 1;
				if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
					currPage = vo.getCurrPage();
				}

				Integer total = fromUserInRulDao.getFromUserCountInURLByType(fromUser);

				Page page = PageUtil.createPage(8, currPage, total);
				List<JsFromUser> list = fromUserInRulDao.getFromUserInURLByType(fromUser, page);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				TgUserinfo userinfo = new TgUserinfo();
				TgSource source = new TgSource();
				if (list != null && list.size() > 0) {
					for (JsFromUser user : list) {
						if (user.getCreateTime() != null) {
							user.setcDate(sdf.format(new Date(user.getCreateTime() * 1000)));
						}
						if (user.getUsername() != null && !"".equals(user.getUsername())) {
							userinfo.setUsername(user.getUsername());
							List<TgUserinfo> userlist = userinfoDao.findByExample(userinfo);
							if (userlist != null && userlist.size() > 0) {
								user.setNickname(userlist.get(0).getNickname());
								;
							}
						}
						if (user.getSource() != null && !"".equals(user.getSource())) {
							source.setKeyname(user.getSource());
							List<TgSource> sourcelist = sourceDao.findByExample(source);
							if (sourcelist != null && sourcelist.size() > 0) {
								user.setSource(sourcelist.get(0).getName());
								;
							}
						}
					}
				}

				result.setDefaultModel("list", list);
				result.setDefaultModel("page", page);
				result.setDefaultModel("fromUser", fromUser);
				return result;
			}
		});
		return new ModelAndView("system/from_user_url_list", modelMap);
	}

	@RequestMapping(value = "/ask_answer_spcode_list")
	public ModelAndView ask_answer_spcode_list(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String topic = request.getParameter("topic");
				String contact = request.getParameter("contact");
				String address = request.getParameter("address");
				String sname = request.getParameter("sname");
				String source = request.getParameter("source");
				String startTime = request.getParameter("stardate");
				String endTime = request.getParameter("enddate");
				String state = request.getParameter("state");
				String st_type = request.getParameter("st_type");

				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

				JsVote vote = new JsVote();
				if (topic != null && !"".equals(topic)) {
					vote.setTopic(topic);
				}
				if (contact != null && !"".equals(contact)) {
					vote.setPhone(contact);
				}
				if (address != null && !"".equals(address)) {
					vote.setCity(address);
				}
				if (sname != null && !"".equals(sname)) {
					vote.setSpname(sname);
				}
				if (source != null && !"".equals(source)) {
					vote.setSource(source);
				}
				if (st_type != null && !"".equals(st_type)) {
					vote.setSt_type(Integer.valueOf(st_type));
				}
				if (startTime != null && !"".equals(startTime)) {
					result.setDefaultModel("startTime", startTime);
					try {
						vote.setStartTime(sdf.parse(startTime).getTime() / 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (endTime != null && !"".equals(endTime)) {
					result.setDefaultModel("endTime", endTime);
					try {
						vote.setEndTime(sdf.parse(endTime).getTime() / 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (state != null && !"".equals(state)) {
					if (state.equals("1")) {
						vote.setState1(state);
					} else if (state.equals("2")) {
						vote.setState2(state);
					}
				}

				Integer currPage = 1;
				if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
					currPage = vo.getCurrPage();
				}
				Integer total = voteDao.getVoSpcodeCountByType(vote);
				Page page = PageUtil.createPage(8, currPage, total);
				List<JsVote> list = voteDao.getVoSpcodelistByType(vote, page);
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				if (list != null && list.size() > 0) {
					for (JsVote vo : list) {
						vo.setCreateTime(sdf1.format(new Date(vo.getCdate() * 1000)));
					}
				}

				result.setDefaultModel("vote", vote);
				result.setDefaultModel("page", page);
				result.setDefaultModel("contact", contact);
				result.setDefaultModel("source", source);
				result.setDefaultModel("address", address);
				result.setDefaultModel("list", list);
				result.setDefaultModel("topic", topic);
				result.setDefaultModel("startTime", startTime);
				result.setDefaultModel("endTime", endTime);
				result.setDefaultModel("sname", sname);
				return result;
			}
		});
		return new ModelAndView("system/ask_answer_spcode_list", modelMap);
	}

	// 销售主管手下员工
	@RequestMapping(value = "/usernamesList")
	public ModelAndView periodList(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");
				String date = request.getParameter("date");

				DecimalFormat fnum = new DecimalFormat("#.#");
				TgUsernames usernames = new TgUsernames();

				if (nickname != null && !"".equals(nickname)) {
					usernames.setNickname(nickname);
				}
				if (date != null && !"".equals(date)) {
					usernames.setDate(date);
				}
				Integer currPage = vo.getCurrPage() == null ? 1 : vo.getCurrPage();
				Integer total = usernamesDao.getUsernamesCountByType(usernames);
				Page page = PageUtil.createPage(8, currPage, total);
				List<TgUsernames> list = usernamesDao.getUsernamesByType(usernames, page);
				if (list != null && list.size() > 0) {

					TgUser user2 = new TgUser();
					TgUser user3 = new TgUser();
					TgReception reception = new TgReception();
					TgReserve reserve = new TgReserve();
					for (TgUsernames u : list) {
						Date start = null;
						Date end = null;
						List<Date> dlist = DateUtil.getMdatefandl(u.getDate());
						if (dlist != null && dlist.size() > 0) {
							start = dlist.get(0);
							end = dlist.get(1);
						}
						String[] users = u.getUsernames().split(",");
						if (users != null && !"".equals(users)) {
							Double cdl = 0.0;
							Integer shidaoAll;
							String msg = "";
							for (int i = 0; i < users.length; i++) {
								if (users[i] != null && !"".equals(users[i])) {
									user2.setUsername(users[i]);
									user2.setIs_zhuanz("排除未转正");
									user3 = userDao.getUserInfoByType(user2);
									if (user3 != null) {
										reception.setStartTime(start.getTime() / 1000);
										reception.setEndTime(end.getTime() / 1000);
										reserve.setStart(start.getTime() / 1000);
										reserve.setEnd(end.getTime() / 1000);
										reception.setUsername(users[i]);
										reserve.setUsername(users[i]);
										Double a = (double) receptionDao.getDanReceptionCount(reception);
										Double b = (double) receptionService.getZonlReceptionCount(reception);
										if (b == 0.0 || b == 0) {
											cdl = 0.0;
										} else {
											cdl = (a / b);
										}

										shidaoAll = reserveDao.getShidaoReserveCountBy(reserve);

										msg += user3.getNickname() + ":成单(" + a + "),接待(" + b + "),成单率("
												+ fnum.format(cdl) + "),实到人数(" + shidaoAll + ")<br/>";
										u.setNicknames(msg);
									}
								}
							}

						}
					}
				}

				TgUser user2 = new TgUser();
				user2.setJob("课程顾问");
				List<TgUser> ulist = userDao.getUserInfoByType2(user2);

				result.setDefaultModel("ulist", ulist);
				result.setDefaultModel("list", list);
				result.setDefaultModel("page", page);
				return result;
			}
		});
		return new ModelAndView("consultant/mark/usernamesList", modelMap);
	}

	// 删除销售主管下属
	@RequestMapping(value = "/delate_usernames")
	public ModelAndView delate_usernames(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");

				if (id != null && !"".equals(id)) {
					usernamesDao.delete(TgUsernames.class, Long.valueOf(id));
					result.setDefaultModel("msg", "1");
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 添加销售主管手下员工
	@RequestMapping(value = "/add_usernames")
	public ModelAndView add_usernames(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");
				String username = request.getParameter("username");
				String date = request.getParameter("date");
				String usernames = request.getParameter("usernames");
				String pusernames = request.getParameter("pusernames");

				TgUsernames userns = new TgUsernames();
				userns.setDate(date);
				userns.setUsername(username);
				if (id != null && !"".equals(id)) {
					userns.setId(Long.valueOf(id));
					userns.setUsernames(usernames);
					userns.setUsernames2(pusernames);
					usernamesDao.update(userns);
					result.setDefaultModel("msg", "1");
				} else {
					List<TgUsernames> list = usernamesDao.findByExample(userns);
					if (list != null && list.size() > 0) {
						result.setDefaultModel("msg", "0");
					} else {
						userns.setCreate_time(new Date());
						userns.setUsernames(usernames);
						userns.setUsernames2(pusernames);
						usernamesDao.insert(userns);
						result.setDefaultModel("msg", "1");
					}
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	// 一键生成手下员工
	@RequestMapping(value = "/add_usernames_bydate")
	public ModelAndView add_usernames_bydate(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String date1 = request.getParameter("date1");
				String date2 = request.getParameter("date2");

				if (date1 != null && !"".equals(date1) && date2 != null && !"".equals(date2)) {
					TgUsernames username = new TgUsernames();
					username.setDate(date1);
					List<TgUsernames> list = usernamesDao.findByExample(username);
					if (list != null && list.size() > 0) {
						for (TgUsernames u : list) {
							username.setDate(date2);
							username.setUsername(u.getUsername());
							if (usernamesDao.countEqual(username) < 1L) {
								username.setUsernames(u.getUsernames());
								username.setUsernames2(u.getUsernames2());
								username.setCreate_time(new Date());
								usernamesDao.insert(username);
								result.setDefaultModel("msg", "1");
							} else {
								result.setDefaultModel("msg", "2");
							}
						}
					} else {
						result.setDefaultModel("msg", "0");
					}
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	@RequestMapping(value = "/get_usernames")
	public ModelAndView get_usernames(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");

				if (id != null && !"".equals(id)) {
					TgUsernames usernames = usernamesDao.findById(TgUsernames.class, Long.valueOf(id));
					result.setDefaultModel("usernames", usernames);
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
	}

	/*
	 * 团队pk
	 * */	
	@RequestMapping(value = "/getPerformanceAmount")
	public ModelAndView getPerformanceAmount(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");

				String date = request.getParameter("date");
				String start = "";
				String end = "";
				Date end1 = null;
				Date start1 = null;
				String start2 = "";
				String start3 = "";
				Date start22 = null;
				Date start23 = null;
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				TgUsernames usernames = new TgUsernames();

				if (nickname != null && !"".equals(nickname)) {
					usernames.setNickname(nickname);
				}
				if (date != null && !"".equals(date)) {
					List<String> li = DateUtil.printWeekdays(date);
					if (li != null && li.size() > 0) {
						start = li.get(0);
						start3 = li.get(5);// 假如周六在哪个月就算在那个月
						end = li.get(li.size() - 1);
						String sta[] = start3.split("-");
						String ed[] = end.split("-");
						if (sta[1] != ed[1]) {
							try {
								start2 = sta[0] + "-" + sta[1] + "-01";
								// 月累計開始時間
								start22 = DateUtil.getBeforetwoDay5(sdf.parse(start2));
								// 月累計結束時間
								start23 = DateUtil.getTommorrowDay3(sdf.parse(end));
								usernames.setDate(ed[0] + "-" + ed[1]);
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					try {
						Date f = sdf.parse(date);
						List<Date> ss = DateUtil.dateToWeek(f);
						if (ss != null && ss.size() > 0) {
							end1 = ss.get(ss.size() - 1);
							start1 = ss.get(0);
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
				} else {
					Date d = new Date();
					String da = sdf.format(d);
					List<String> li = DateUtil.printWeekdays(da);
					if (li != null && li.size() > 0) {
						start = li.get(0);
						start3 = li.get(2);// 假如有5天以上在下个月算到下个月
						end = li.get(li.size() - 1);
						String sta[] = start3.split("-");
						String ed[] = end.split("-");
						if (sta[1] != ed[1]) {
							try {
								start2 = sta[0] + "-" + sta[1] + "-01";
								// 月累計開始時間
								start22 = DateUtil.getBeforetwoDay3(sdf.parse(start2));
								// 月累計結束時間
								start23 = DateUtil.getTommorrowDay3(sdf.parse(end));
								usernames.setDate(ed[0] + "-" + ed[1]);
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					List<Date> ss = DateUtil.dateToWeek(d);
					if (ss != null && ss.size() > 0) {
						end1 = ss.get(ss.size() - 1);
						start1 = ss.get(0);
					}

				}

				List<TgPerformanceAmount> lt = new ArrayList<TgPerformanceAmount>();

				List<TgUsernames> list = usernamesDao.getUsernamesByType3(usernames);
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getUsername().equals("sunmengmeng")) {
						list.remove(i);
					}
				}
				TgMoney mon = new TgMoney();
				TgReception reception = new TgReception();
				if (list != null && list.size() > 0) {
					for (int i = 0; i < list.size(); i++) {
						TgPerformanceAmount tgPerformanceAmount = new TgPerformanceAmount();
						String users = list.get(i).getUsernames();// 属下列表
						String users1 = list.get(i).getUsernames2();// 属下列表用于评分
						String[] u1 = users.split(",");
						String[] u2 = users1.split(",");
						List<String> ulist1 = Arrays.asList(u1);
						List<String> ulist2 = Arrays.asList(u2);

						Set s = new TreeSet(ulist1);// 融合后的的两个下属集合
						for (String a : ulist2) {
							s.add(a);
						}
						Object[] ary = s.toArray();
						String str = StringUtils.join(ary, ",");// 融合后的的两个下属集合 字符串

						users = list.get(i).getUsername() + "," + str;
						if (users != null && !"".equals(users)) {
							String user1[] = users.split(",");
							Double money = (double) 0;
							Integer tot = 0;
							for (int c = 0; c < user1.length; c++) {
								mon.setStart(start);
								mon.setEnd(end);
								mon.setUsername(user1[c]);
								List<TgMoney> ls = moneyDao.getMoneysByType5(mon);
								reception.setStartTime(start1.getTime() / 1000);
								reception.setEndTime(end1.getTime() / 1000);
								reception.setUsername(user1[c]);
								int total1 = receptionDao.getZonlReceptionCount1(reception);
								if (user1[c] == null || user1[c].equals("")) {
									total1 = 0;
									ls = null;
								}
								tot = total1 + tot;
								if (ls != null && ls.size() > 0) {
									for (int j = 0; j < ls.size(); j++) {
										mon.setStu_id(ls.get(j).getStu_id());
										mon.setUsername(ls.get(j).getUsername());
										List<TgMoney> ls1 = moneyDao.getMoneysByType6(mon);
										if (ls1 != null && ls1.size() > 0) {
											if (ls1.size() >= 2) {
												if (ls1.get(ls1.size() - 2).getId() == ls.get(j).getId()) {
													ls.get(j).setMoneys(ls.get(j).getMoneys()
															+ ls1.get(ls1.size() - 2).getMoneys());
												}
											}
										}
										if (ls.get(j).getMoneys() != null) {
											money = money + ls.get(j).getMoneys();
										}
									}
								}
							}
							if (tot <= 10) {
								tgPerformanceAmount.setMark(0);
								if (tot != 0) {
									tgPerformanceAmount.setY(money / tot);
								} else {
									tgPerformanceAmount.setY((double) 0);
								}
							} else {
								Integer mark = (int) (money / tot);
								tgPerformanceAmount.setY(money / tot);
								if (mark > 0 && mark < 5000) {
									tgPerformanceAmount.setMark(1);
								} else if (mark >= 5000 && mark < 6000) {
									tgPerformanceAmount.setMark(2);
								} else if (mark > 6000 && mark <= 7000) {
									tgPerformanceAmount.setMark(3);
								} else if (mark > 7000 && mark <= 8000) {
									tgPerformanceAmount.setMark(4);
								} else if (mark > 8000 && mark <= 9000) {
									tgPerformanceAmount.setMark(5);
								} else if (mark > 9000 && mark <= 10000) {
									tgPerformanceAmount.setMark(6);
								} else if (mark > 10000 && mark <= 11000) {
									tgPerformanceAmount.setMark(7);
								} else if (mark > 11000) {
									tgPerformanceAmount.setMark(8);
								}
							}
							tgPerformanceAmount.setMoney(money);
							tgPerformanceAmount.setTotal(tot);
							tgPerformanceAmount.setUsername(list.get(i).getNickname());
							tgPerformanceAmount.setEnd(end1);
							tgPerformanceAmount.setStart(start1);
							tgPerformanceAmount.setTimeEnd(end);
							tgPerformanceAmount.setTimeStart(start);
							tgPerformanceAmount.setTimeStart(start);
							tgPerformanceAmount.setLogin_name(list.get(i).getUsername());
							List<TgPerformanceAmount> list1 = performanceAcountDao
									.getPerformanceAcountByType1(tgPerformanceAmount);
							if (list1 != null && list1.size() > 0) {
								tgPerformanceAmount.setId(list1.get(0).getId());
								performanceAcountDao.update(tgPerformanceAmount);
							} else {
								performanceAcountDao.insert(tgPerformanceAmount);
							}
						}
						lt.add(tgPerformanceAmount);
					}
				}

				TgPerformanceAmount tg = new TgPerformanceAmount();
				tg.setTimeStart(sdf.format(start22));
				tg.setTimeEnd(sdf.format(start23));
				List<TgPerformanceAmount> lst = performanceAcountDao.getPerformanceAcountByType3(tg);
				tg.setTimeStart(start);
				List<TgPerformanceAmount> lst3 = performanceAcountDao.getPerformanceAcountByType1(tg);
				tg.setTime(start + "至" + end);
				result.setDefaultModel("performanceAcount", tg);
				if (lst != null && lst.size() > 0) {
					for (int f = 0; f < lst.size(); f++) {
						if (lst3 != null && lst3.size() > 0) {
							for (int t = 0; t < lst3.size(); t++) {
								if (lst3.get(t).getUsername().equals(lst.get(f).getUsername())) {
									lst3.get(t).setAll(lst.get(f).getMark());
									if (lst3.get(t).getUsername().equals("孙蒙蒙")) {
										lst3.remove(t);
									}
									break;
								}
							}
						}
					}
				}

				for (int i = 0; i < lst3.size(); i++) {
					for (int j = 0; j < lst3.size(); j++) {
						TgPerformanceAmount amount = new TgPerformanceAmount();
						if (lst3.get(i).getAll() == null) {
							lst3.get(i).setAll(0);
						}
						if (lst3.get(j).getAll() == null) {
							lst3.get(j).setAll(0);
						}

						if (lst3.get(i).getAll() > lst3.get(j).getAll()) {
							amount.setMark(lst3.get(i).getMark());
							amount.setAll(lst3.get(i).getAll());
							amount.setMoney(lst3.get(i).getMoney());
							amount.setTotal(lst3.get(i).getTotal());
							amount.setY(lst3.get(i).getY());
							amount.setUsername(lst3.get(i).getUsername());
							lst3.get(i).setAll(lst3.get(j).getAll());
							lst3.get(i).setMark(lst3.get(j).getMark());
							lst3.get(i).setMoney(lst3.get(j).getMoney());
							lst3.get(i).setTotal(lst3.get(j).getTotal());
							lst3.get(i).setY(lst3.get(j).getY());
							lst3.get(i).setUsername(lst3.get(j).getUsername());
							lst3.get(j).setAll(amount.getAll());
							lst3.get(j).setMark(amount.getMark());
							lst3.get(j).setMoney(amount.getMoney());
							lst3.get(j).setTotal(amount.getTotal());
							lst3.get(j).setY(amount.getY());
							lst3.get(j).setUsername(amount.getUsername());

						}

					}
				}
				result.setDefaultModel("list", lst3);
				return result;
			}
		});
		return new ModelAndView("consultant/mark/performance_acount_list", modelMap);
	}

	/**
	 * @Description:成长咨询学员列表
	 * @author: Hqd
	 * @date: 2017年11月29日 上午9:39:46
	 * @return
	 */
	@RequestMapping(value = "/getZXstudent")
	public ModelAndView getZXstudent(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");
				String stuname = request.getParameter("stuname");

				TgUser user = UserUtil.getUserFromCookies();

				try {
					nickname = URLEncoder.encode(user.getNickname(), "utf-8");

					if (stuname != null && !"".equals(stuname) && !"null".equals(stuname)) {
						stuname = URLEncoder.encode(stuname, "utf-8");
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String url = "http://www.yueqiao.org/web/teaweb/getZxStudent.json?teach_id2=" + nickname + "&stuname="
						+ stuname;
				String teacher = HttpUtil.doGet(url, "", "utf-8", true);
				JSONObject obj;
				try {
					obj = new JSONObject(teacher);
					JSONArray listobj = obj.getJSONArray("list");
					List<TgStudent> tlist = new ArrayList<TgStudent>();
					for (int i = 0; i < listobj.length(); i++) {
						TgStudent tuser = new TgStudent();
						JSONObject o = new JSONObject(listobj.get(i).toString());
						if (!o.isNull("name")) {
							tuser.setStudent(o.getString("name"));
						}
						/**
						 * 训练老师的id
						 */
						if (!o.isNull("teach_id")) {
							/*Teacher tea = teacherDao.findById(Teacher.class, Long.parseLong(o.getString("teach_id")));
							if (tea != null) {
								tuser.setTrainTeacher(tea.getName());
							}*/
							String url0 = "http://www.yueqiao.org/web/teaweb/laoshiList2.json?teach_id=" + Long.parseLong(o.getString("teach_id"));
							String teacher0 = HttpUtil.doGet(url0, "", "utf-8", true);
							JSONObject obj0 = new JSONObject(teacher0);
							JSONArray listobj0 = obj0.getJSONArray("list");
							JSONObject teacher2 = new JSONObject(listobj0.get(0).toString());
							tuser.setTrainTeacher(teacher2.getString("name"));
						} else {
							if (!o.isNull("teach_id2")) {// 所属老师
								tuser.setTrainTeacher(o.getString("teach_id2"));
							}
						}
						if (!o.isNull("situation")) {
							tuser.setState(o.getString("situation"));
						}
						if (!o.isNull("center")) {
							tuser.setRegion(o.getString("center"));
						}
						if (!o.isNull("stu_id")) {
							tuser.setId(Long.valueOf(o.getString("stu_id")));
							// 获取该学员的当前学习的课时数
							// String url_newBest =
							// "http://192.168.122.129:82/web/teaweb/gettudentecordsNewBest.json?stu_id="+o.getString("stu_id");
							String url_newBest = "http://www.yueqiao.org/web/teaweb/gettudentecordsNewBest.json?stu_id="
									+ o.getString("stu_id");
							String record = HttpUtil.doGet(url_newBest, "", "utf-8", true);
							JSONObject obj_newBest = new JSONObject(record);
							if (!obj_newBest.isNull("record")) {
								if (!obj_newBest.getJSONObject("record").isNull("times"))
									// 现在正在上的课时数
									tuser.setTimes(
											Integer.parseInt(obj_newBest.getJSONObject("record").getString("times")));
								if (!obj_newBest.getJSONObject("record").isNull("zontimes"))
									// 总课时数
									tuser.setZontimes(obj_newBest.getJSONObject("record").getString("zontimes"));
							}
						}
						if (!o.isNull("teach_id2")) {
							tuser.setNickname(o.getString("teach_id2"));
						}
						if (!o.isNull("create_time")) {
							tuser.setBirthdays(o.getString("create_time"));
						}
						tlist.add(tuser);
					}

					result.setDefaultModel("tlist", tlist);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				return result;
			}
		});
		return new ModelAndView("teaching/cz_student_list", modelMap);
	}

	/**
	 * @Title: 方法名：getStu_xljl
	 * @Description: 功能描述：获取学员所有训练记录
	 * @author hqD ☺ ^o^
	 * @date 2017年12月12日 下午4:43:10
	 * @param @param modelMap
	 * @param @param vo
	 * @param @param request
	 * @param @param response
	 * @param @return
	 * @param @throws Exception    
	 * @return ModelAndView    返回类型
	 */
	@RequestMapping(value = "/getStu_xljl")
	public ModelAndView getStu_xljl(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					String stuName = request.getParameter("stuName");
					stuName = URLEncoder.encode(stuName, "utf-8");
					String url = "http://www.yueqiao.org/web/teaweb/getStu_xljl.json?stuName=" + stuName;
					//String url = "http://192.168.122.129:82/web/teaweb/getStu_xljl.json?stuName=" + stuName;
					String cords = HttpUtil.doGet(url, "", "utf-8", true);
					JSONObject obj;
					obj = new JSONObject(cords);
					JSONArray listobj = obj.getJSONArray("records");
					List<Record> list = new ArrayList<Record>();  
					for (int i = 0; i < listobj.length(); i++) {
						JSONObject o = new JSONObject(listobj.get(i).toString());
						Record record = new Record();
						if (!o.isNull("tea_name")) {
							record.setTea_name(o.getString("tea_name"));
						}
						if (!o.isNull("useful_time")) {
							Long userfulTime = Long.parseLong(o.getString("useful_time"));
							record.setUseful_time(DateUtil.LongToDate(userfulTime));
						}
						if (!o.isNull("xltime")) {
							record.setXltime(o.getString("xltime"));
						}
						if (!o.isNull("course_type")) {
							record.setCourse_type(o.getString("course_type"));
						}
						if (!o.isNull("times")) {
							record.setTimes(o.getString("times"));
						}
						if (!o.isNull("teacher")) {
							record.setTeacher(o.getString("teacher"));
						}
						if (!o.isNull("teachertwo")) {
							record.setTeachertwo(o.getString("teachertwo"));
						}
						if (!o.isNull("biaoxian")) {
							record.setBiaoxian(o.getString("biaoxian"));
						}
						if (!o.isNull("say")) {
							record.setSay(o.getString("say"));
						}
						if (!o.isNull("fankui")) {
							record.setFankui(o.getString("fankui"));
						}
						if (!o.isNull("goutong")) {
							record.setGoutong(o.getString("goutong"));
						}
						list.add(record);
					}
					result.setDefaultModel("list", list);
				}catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("teaching/student_xljl", modelMap);
	}

}
